package com.algorithm.list;


public class LinkedList {

	Node mNodeHead;
	Node mNodeLasted;

	public void insert(Node mNode){
		if(mNodeHead==null){
			mNodeHead = mNode;
		}else{
			Node mTempNode = mNodeHead;
			while(mTempNode.next!=null){
				mTempNode = mTempNode.next;
			}
			mTempNode.next = mNode;
		}


		mNodeLasted = mNode;
	}

	//如果是单链的话 查找其前面一个元素的话 还需要遍历，否则没有关联项
	public void remove(Node mNode){

		if(mNode == mNodeHead){
			mNodeHead = mNodeHead.next;
			return;
		}

		Node  mNodeTempPre = mNodeHead;
		Node  mNodeTempNext = null;
		while(mNodeTempPre.next!=null){
			if(mNodeTempPre.next==mNode){
				//把当前的置空，然后把当前的上一个
				mNodeTempNext = mNodeTempPre.next.next;
				break;
			}
			mNodeTempPre = mNodeTempPre.next;
		}

		mNodeTempPre.next= mNodeTempNext;




	}





	public int get(Node mNode){
		return -1;
	}



	static class Node{
		public String value;

		public Node next;

	}

	@Override
	public String toString() {
		StringBuilder mBuilder = new StringBuilder();
		Node mNodeInteRupt = mNodeHead;
		while(mNodeInteRupt!=null){
			mBuilder.append(mNodeInteRupt.value);
			mNodeInteRupt = mNodeInteRupt.next;
			if(mNodeInteRupt!=null)
			mBuilder.append("->");
		}
		return mBuilder.toString();
	}


	public static void main(String args[]){
		LinkedList mLinkedList = new LinkedList();
		Node mNode = new Node();
		mNode.value = "first";
		mLinkedList.insert(mNode);

		Node mNode1 = new Node();
		mNode1.value = "second";
		mLinkedList.insert(mNode1);

		Node mNode2 = new Node();
		mNode2.value = "third";
		mLinkedList.insert(mNode2);

		Node mNode3 = new Node();
		mNode3.value = "fourth";
		mLinkedList.insert(mNode3);

		Node mNode4= new Node();
		mNode4.value = "fifth";
		mLinkedList.insert(mNode4);

		System.out.print(mLinkedList.toString());
	}
}
